﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace a29_MagnifyingGlass
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private static readonly double DistanceFromMouse = 5;

        private void MagnifyingPosChanged(object sender, MouseEventArgs e)
        {
            Mouse.SetCursor(Cursors.Cross);

            // Get the current position of the mouse pointer.
            var currentMousePosition = e.GetPosition(this);

            // Determine whether the magnifying glass should be shown to the
            // the left or right of the mouse pointer.
            if (ActualWidth - currentMousePosition.X > magnifyingGlass.Width + DistanceFromMouse)
            {
                Canvas.SetLeft(magnifyingGlass, currentMousePosition.X + DistanceFromMouse);
            }
            else
            {
                Canvas.SetLeft(magnifyingGlass, currentMousePosition.X - DistanceFromMouse - magnifyingGlass.Width);
            }

            // Determine whether the magnifying glass should be shown 
            // above or below the mouse pointer.
            if (ActualHeight - currentMousePosition.Y > magnifyingGlass.Height + DistanceFromMouse)
            {
                Canvas.SetTop(magnifyingGlass, currentMousePosition.Y + DistanceFromMouse);
            }
            else
            {
                Canvas.SetTop(magnifyingGlass, currentMousePosition.Y - DistanceFromMouse - magnifyingGlass.Height);
            }


            //发达率是 magnifyingGlass.size / viewbox.size
            // Update the visual brush's Viewbox to magnify a 20 by 20 rectangle,
            // centered on the current mouse position.
            myVisual.Viewbox =  new Rect(currentMousePosition.X - 10, currentMousePosition.Y - 10, 50, 50);
        }
    }
}
